// pages/shou_ye/shou_ye.js
import {request} from "../../utils/http.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    carousel:null,
    list:[
      {
        url:"http://122.51.84.231:9001/dianshang/今日新品.png",
        text:"今日新品"
      },
      {
        url:"http://122.51.84.231:9001/dianshang/水果区.png",
        text:"水果区"
      },
      {
        url:"http://122.51.84.231:9001/dianshang/蔬菜区.png",
        text:"蔬菜区"
      },
      {
        url:"http://122.51.84.231:9001/dianshang/水产区.png",
        text:"水产区"
      },
      {
        url:"http://122.51.84.231:9001/dianshang/肉类区.png",
        text:"肉类区"
      },
      {
        url:"http://122.51.84.231:9001/dianshang/蛋类区.png",
        text:"蛋类区"
      },
      {
        url:"http://122.51.84.231:9001/dianshang/豆制区.png",
        text:"豆制品"
      },
      {
        url:"http://122.51.84.231:9001/dianshang/辣条.jpg",
        text:"水果区"
      },
      {
        url:"http://122.51.84.231:9001/dianshang/薯片.jpg",
        text:"蔬菜区"
      },
      {
        url:"http://122.51.84.231:9001/dianshang/饮料.jpg",
        text:"水产区"
      },
    ],
    goods1:[
      {
        url:"http://122.51.84.231:9001/dianshang/鸡蛋.png",
        text:"语农 散装土鸡蛋 360枚 40斤",
        time:"08月13日",
        money:"28.8",
        nums:2700
      },
      {
        url:"http://122.51.84.231:9001/dianshang/鸡蛋.png",
        text:"语农 散装土鸡蛋 360枚 40斤",
        time:"08月13日",
        money:"28.8",
        nums:2700
      },
      {
        url:"http://122.51.84.231:9001/dianshang/鸡蛋.png",
        text:"语农 散装土鸡蛋 360枚 40斤",
        time:"08月13日",
        money:"28.8",
        nums:2700
      },
    ],
    jingxuan:[
      "http://122.51.84.231:9001/dianshang/ajkshdjashdjhw.png",
      "http://122.51.84.231:9001/dianshang/asjihgdajhsdasd.png"
    ],
    show:true
  },
  gobay(e){
    request({
      url:"/getInfo",
      header: {
        "authorization": getApp().globalData.authorization
      }
    }).then(data=>{
      console.log(data);
      if(data.code==200){
        request({
          url:"/dianshang/cart",
          method:"POST",
          data:{
            goodsid:this.data.goods1[e.currentTarget.id].id,
            nums:1,
            userid:getApp().globalData.userInfo.userId,
            del:0
          },
          header: {
            "authorization": getApp().globalData.authorization
          }
        }).then(data1=>{
          if(data1.code==200){
            wx.showModal({
              title: data.msg,
              showCancel: false
            });
            getApp().globalData.nums++;
            wx.setTabBarBadge({
              index:2,
              text:getApp().globalData.nums.toString(),
            })
          }
        })
      }else{
        wx.navigateTo({
          url: '/pages/deng_lu/deng_lu',
        })
      }
    })
   
  },
  close(){
    this.setData({
      show:false
    })
  },
  to(res){
    wx.navigateTo({
      url: res.currentTarget.id,
    })
  },
  tobar(e){
    wx.reLaunch({
      url:e.currentTarget.id
    })
  },
  


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
    request({
      url:"/dianshang/carousel1/list?del=0"
    }).then((data)=>{
      this.setData({
        carousel:data.rows
      });
    })

    request({
      url: "/system/fl/list"
    }).then((data)=>{
      for(let i = 0;i < 10 ;i++){
        this.data.list[i]={...data.rows[i],...this.data.list[i]}
      }
      //  console.log(this.data.list);
      this.setData({
        list:this.data.list
      })
     
    })

    request({
      url:"/dianshang/goods/list?pageNum=1&pageSize=5"
    }).then((data)=>{
      // console.log(data);
      this.setData({
        goods1:data.rows
      });
    })
    
    request({
      url:"/dianshang/carousel/list"
    }).then((data)=>{
      // console.log(data);
      this.setData({
        jingxuan:data.rows
      });
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getnums();
    request({
      url:"/getInfo",
      header: {
        "authorization": getApp().globalData.authorization
      }
    }).then(data=>{
      if(data.code==200){
        this.setData({
          show:false
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  getnums(){
    
    request({
      url:"/getInfo",
      header: {
        "authorization": getApp().globalData.authorization
      }
    }).then(data=>{
      if(data.code==200){
        request({
          url:"/dianshang/cart/list?userid="+getApp().globalData.userInfo.userId+"&pageSize=99",
          header: {
            "authorization": getApp().globalData.authorization
          }
        }).then(data1=>{
          console.log(data1);
          getApp().globalData.nums=data1.total;
          if(getApp().globalData.nums==0){
            wx.removeTabBarBadge({
              index:2
            })
          }else{
            wx.setTabBarBadge({
              index:2,
              text:getApp().globalData.nums.toString(),
              fail(err){
                console.log(err);
                console.log(getApp());
              }
            })
          }
        })
      }else{
        getApp().globalData.nums=0;
        wx.removeTabBarBadge({
          index:2
        })
      }
    })
    
  }
})